package com.abbas.CayTcore.presenter

import android.app.Activity
import android.app.ProgressDialog
import com.abbas.CayTcore.CoreApp
import com.abbas.CayTcore.base.BasePresenter
import com.abbas.CayTcore.data.CoreOriginData
import com.abbas.CayTcore.model.MainModel
import com.abbas.CayTcore.ui.MainActivity
import com.abbas.CayTcore.view.ViewContract

class MainPresenter : BasePresenter<ViewContract.MainActivityView, MainModel>() {

    override fun initModel(): MainModel {
        return MainModel()
    }

    fun firstInit() {
        CoreApp.apply {
            val first = sp.getBoolean("first", true)
            if (first) {
                val dialog = ProgressDialog(view.get() as Activity).apply {
                    setTitle("初始化数据中...")
                    setCancelable(false)
                    view.get()?.showWaitingDialog(this)
                }
                Thread {
                    Thread.sleep(2000)

                    CoreOriginData().core.forEach {
                        db.coreDao().insert(it)
                    }
                    sp.edit()
                        .putBoolean("first", false)
                        .apply()

                    handler.post{
                        view.get()?.dismissDialog(dialog)
                    }
                }.start()
            }
        }
    }
}